package cn.xluobo.business.wechat.miniapp.student.service;

import cn.xluobo.business.sc.student.domain.req.ReqSearchScStudent;
import cn.xluobo.business.sc.student.domain.req.ReqStudentSelect;
import cn.xluobo.business.sc.student.domain.resp.RespSearchStudent;
import cn.xluobo.business.sc.student.repo.mapper.ScStudentMapper;
import cn.xluobo.business.wechat.miniapp.student.domain.resp.RespIndexBarStudent;
import cn.xluobo.config.login.LoginUser;
import cn.xluobo.core.utils.pinyin.PinYinUtil;
import cn.xluobo.utils.LoginUserUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：KingLong
 * @date ：Created in 2020/11/11 19:13
 */
@Service
public class WechatMiniStudentService {

    @Autowired
    private ScStudentMapper studentMapper;


    /**
     * index bar 学生列表
     * @return
     */
    public List<RespIndexBarStudent> searchIndexBarStudentList() {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        Long deptId = loginUser.getDeptId();

        List<RespIndexBarStudent> indexBarStudentList = Lists.newArrayList();

        Map<String, List<RespSearchStudent>> indexBarMap = Maps.newHashMap();

        // 全部学生列表
        ReqSearchScStudent reqSearchScStudent = new ReqSearchScStudent();
        reqSearchScStudent.setDeptId(deptId);
        reqSearchScStudent.setUserId(loginUser.getUserId());

        List<RespSearchStudent> studentList = studentMapper.selectAllStudent(reqSearchScStudent);
        for (RespSearchStudent student : studentList) {
            String studentName = student.getStudentName();
            String pinYin = PinYinUtil.getPinYin(studentName).substring(0,1).toUpperCase();
            if(indexBarMap.containsKey(pinYin)) {
                List<RespSearchStudent> indexListData = indexBarMap.get(pinYin);
                indexListData.add(student);
            } else {
                List<RespSearchStudent> indexListData = Lists.newArrayList();
                indexListData.add(student);
                indexBarMap.put(pinYin, indexListData);
            }
        }
        indexBarMap.forEach((key,value) -> {
            indexBarStudentList.add(new RespIndexBarStudent(key,value));
        });
        return indexBarStudentList;
    }
}
